<?php
namespace app\api\model;
use think\Config;
use think\Db;
class UserModel extends \app\common\model\CommonModel
{
    
    /**
     * @param string $username
     * @param string $password
     */
    public function find($username,$password)
    {
        try {
            $param = ['username' => $username,'password' => $password];
            $url = "https://fc.souxiu.cn/oaInterface/user?".http_build_query($param);
            $res = httpRequest($url);
            if(!empty($res['code']) && $res['code'] == 200 && $res['msg'] == 'success'){
                $info = [
                    'username'      => $res['data'][0]['username'],
                    'userid'        => $res['data'][0]['userid'],
                    'post'          => $res['data'][0]['post'],
                    'mail'          => $res['data'][0]['mail'],
                    'department'    => $res['data'][0]['department'],
                    'company'       => $res['data'][0]['company'],
                    'departmentId'  => $res['data'][0]['orgDepartmentId'],
                    'postId'        => $res['data'][0]['orgPostId'],
                    'active_time'   => date('Y-m-d H:i:s'),
                    'last_login'    => date('Y-m-d H:i:s'),
                ];
                $exist = Db::table('user')->where(['userid' => $info['userid']])->find();
                // Db::table('log')->insert(['userid' => $info['userid'],'event' => 'login','event_time' => date('Y-m-d H:i:s'),'status' => 'success','desc' => '登录成功']);
                if(!$exist){
                    Db::table('user')->insert($info);
                    return $info;    
                }
                Db::table('user')->where(['userid' => $info['userid']])->update($info);
                return $info;
            }
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
    
}